package com.xoa.service.attend;

import com.xoa.dao.attend.AttendHolidayMapper;
import com.xoa.dao.attend.AttendSetMapper;
import com.xoa.dao.attendance.AttendScheduleMapper;
import com.xoa.dao.attendance.UserDutyMapper;
import com.xoa.dao.users.UserExtMapper;
import com.xoa.dao.users.UsersMapper;
import com.xoa.model.attend.AttendSet;
import com.xoa.model.attendance.UserDuty;
import com.xoa.model.users.Users;
import com.xoa.util.CookiesUtil;
import com.xoa.util.DateFormat;
import com.xoa.util.ToJson;
import com.xoa.util.attend.AttendDateUtil;
import com.xoa.util.common.StringUtils;
import com.xoa.util.common.session.SessionUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
import java.util.*;

/**
 * Created by gsb on 2017/6/6.
 */
@Service
@SuppressWarnings("all")
public class AttendSetService {
    @Autowired
    private AttendSetMapper attendSetMapper;
    @Autowired
    UserDutyMapper userDutyMapper;
    @Autowired
    private UserExtMapper userExtMapper;
    @Autowired
    private UsersMapper usersMapper;
    @Autowired
    private AttendScheduleMapper attendScheduleMapper;
    @Autowired
    AttendHolidayMapper attendHolidayMapper;

    public ToJson<AttendSet> newAttendSet(AttendSet attendSet, HttpServletRequest request) {
        ToJson<AttendSet> toJson = new ToJson<>(1, "err");
        try {
            if (attendSet.getUid() == null) {
                Cookie redisSessionId = CookiesUtil.getCookieByName(request, "redisSessionId");
                Users users = SessionUtils.getSessionInfo(request.getSession(), Users.class, new Users(), redisSessionId);
                attendSet.setUid(users.getUid());
            }
            String str = paramAttendSet(attendSet);
            if (!StringUtils.checkNull(str)) {
                toJson.setFlag(0);
                toJson.setMsg(str);
                return toJson;
            }
            Integer setSum = attendSetMapper.getAttendSetByName(attendSet.getTitle());
            if (setSum > 0) {
                toJson.setFlag(0);
                toJson.setMsg("类型名称已存在");
                return toJson;
            }
            int res = attendSetMapper.addAttendSet(attendSet);
            if (res > 0) {
                toJson.setFlag(0);
                toJson.setMsg("添加成功");
                return toJson;
            }
        } catch (Exception e) {
            e.printStackTrace();
            toJson.setMsg(e.getMessage());
        }
        return toJson;
    }

    public ToJson<AttendSet> updateAttendSet(AttendSet attendSet) {
        ToJson<AttendSet> toJson = new ToJson<>(1, "err");
        try {
            String str = paramAttendSet(attendSet);
            if (!StringUtils.checkNull(str)) {
                toJson.setMsg(str);
                return toJson;
            }
            Integer setSum = attendSetMapper.getAttendSetByName(attendSet.getTitle());
            AttendSet attendSet1 = attendSetMapper.selectAttendSetSid(attendSet.getSid());
            if (!attendSet1.getTitle().equals(attendSet.getTitle()) && setSum > 0) {
                toJson.setMsg("类型名称已存在");
                return toJson;
            }
            int res = attendSetMapper.updateAttendSet(attendSet);
            if (res > 0) {
                toJson.setFlag(0);
                toJson.setMsg("更新成功");
                return toJson;
            }
        } catch (Exception e) {
            e.printStackTrace();
            toJson.setMsg(e.getMessage());
        }
        return toJson;
    }

    /**
     * 查询所有考勤类型
     */
    public ToJson<AttendSet> selectAttendSet() {
        ToJson<AttendSet> toJson = new ToJson<>(1, "err");
        List<AttendSet> attendSetList = attendSetMapper.selectAttendSet();
        if (attendSetList.size() > 0) {
            toJson.setObj(attendSetList);
            toJson.setFlag(0);
            toJson.setMsg("ok");
        }
        return toJson;
    }

    /**
     * 根据ID 获取考勤类型
     */
    public ToJson<AttendSet> queryAttendSetId(Integer sid, HttpServletRequest request) {
        ToJson<AttendSet> toJson = new ToJson<>(1, "err");
        try {
            AttendSet attendSet = attendSetMapper.selectAttendSetSid(sid);
            if (attendSet != null) {
                handleAttendSet(attendSet);
            }
            toJson.setObject(attendSet);
            toJson.setFlag(0);
            toJson.setMsg("true");
        } catch (Exception e) {
            e.printStackTrace();
            toJson.setMsg(e.getMessage());
        }
        return toJson;
    }

    public ToJson<AttendSet> delAttendSetbyId(Integer sid) {
        ToJson<AttendSet> toJson = new ToJson<>(1, "err");
        //注意这里删除时 需要确定有没有这个类型的用户

        int res = attendSetMapper.delAttendSet(sid);
        if (res > 0) {
            toJson.setFlag(0);
            toJson.setMsg("删除成功");
        }
        return toJson;

    }

    //定时器执行，查询用户的考勤信息用于 提醒推送
    public AttendSet queryAttendSetByTuiSongId(Integer uid) {
        AttendSet attendSet = null;
        UserDuty userDuty = userDutyMapper.userDuty(uid, DateFormat.getDatestr(new Date()));
        if (userDuty != null) {
            attendSet = attendSetMapper.selectAttendSetSid(userDuty.getDutyType());
        }
        return attendSet;
    }

    //移动端时间增加秒
    private static String seconds(String data) {
        if (!StringUtils.checkNull(data)) {
            String[] dataStr = data.split(":");
            if (dataStr.length != 3) {
                data = data + ":00";
            }
        }
        return data;
    }

    //处理考勤类型实体类
    public static String paramAttendSet(AttendSet attendSet) {
        String strReturn = null;
        if (StringUtils.checkNull(attendSet.getTitle())) {
            return "考勤签到类型名称不能为空";
        }
        if (StringUtils.checkNull(attendSet.getAttendType())) {
            return "考勤签到类型不能为空";
        }
        if ("0".equals(attendSet.getAttendType())) {
            if (attendSet.getAtime1Setother().equals("1") && StringUtils.checkNull(attendSet.getAtime1()) ||
                    attendSet.getAtime2Setother().equals("1") && StringUtils.checkNull(attendSet.getAtime1()) ||
                    attendSet.getAtime3Setother().equals("1") && StringUtils.checkNull(attendSet.getAtime1()) ||
                    attendSet.getAtime4Setother().equals("1") && StringUtils.checkNull(attendSet.getAtime1()) ||
                    attendSet.getAtime5Setother().equals("1") && StringUtils.checkNull(attendSet.getAtime1()) ||
                    attendSet.getAtime6Setother().equals("1") && StringUtils.checkNull(attendSet.getAtime1())) {
                return "考勤签到时间不能为空";
            }
        } else if ("1".equals(attendSet.getAttendType())) {
            if (StringUtils.checkNull(attendSet.getWorkStart()) || StringUtils.checkNull(attendSet.getWorkEnd()) || attendSet.getWorkHours() == null) {
                return "时间段和最小工作时长不能为空";
            } else {
                Integer integer = AttendDateUtil.computationTime(attendSet.getWorkStart(), attendSet.getWorkEnd(), "HH:mm:ss");
                Integer workHours = attendSet.getWorkHours();
                if (integer <= 0) {
                    return "时间范围不规范";
                } else if (integer < (workHours * 60 * 60)) {
                    return "时间范围无法满足最小工作时长";
                }
            }
        }
        attendSet.setAtime1Setother(StringUtils.checkNull(attendSet.getAtime1Setother()) ? "0" : attendSet.getAtime1Setother());
        attendSet.setAtime2Setother(StringUtils.checkNull(attendSet.getAtime2Setother()) ? "0" : attendSet.getAtime2Setother());
        attendSet.setAtime3Setother(StringUtils.checkNull(attendSet.getAtime3Setother()) ? "0" : attendSet.getAtime3Setother());
        attendSet.setAtime4Setother(StringUtils.checkNull(attendSet.getAtime4Setother()) ? "0" : attendSet.getAtime4Setother());
        attendSet.setAtime5Setother(StringUtils.checkNull(attendSet.getAtime5Setother()) ? "0" : attendSet.getAtime5Setother());
        attendSet.setAtime6Setother(StringUtils.checkNull(attendSet.getAtime6Setother()) ? "0" : attendSet.getAtime6Setother());
        attendSet.setCommute1(StringUtils.checkNull(attendSet.getCommute1()) ? "1" : attendSet.getCommute1());
        attendSet.setCommute2(StringUtils.checkNull(attendSet.getCommute2()) ? "1" : attendSet.getCommute2());
        attendSet.setCommute3(StringUtils.checkNull(attendSet.getCommute3()) ? "1" : attendSet.getCommute3());
        attendSet.setCommute4(StringUtils.checkNull(attendSet.getCommute4()) ? "1" : attendSet.getCommute4());
        attendSet.setCommute5(StringUtils.checkNull(attendSet.getCommute5()) ? "1" : attendSet.getCommute5());
        attendSet.setCommute6(StringUtils.checkNull(attendSet.getCommute6()) ? "1" : attendSet.getCommute6());
        attendSet.setAtime1Set(attendSet.getAtime1Setother() + "|" + attendSet.getCommute1());
        attendSet.setAtime2Set(attendSet.getAtime2Setother() + "|" + attendSet.getCommute2());
        attendSet.setAtime3Set(attendSet.getAtime3Setother() + "|" + attendSet.getCommute3());
        attendSet.setAtime4Set(attendSet.getAtime4Setother() + "|" + attendSet.getCommute4());
        attendSet.setAtime5Set(attendSet.getAtime5Setother() + "|" + attendSet.getCommute5());
        attendSet.setAtime6Set(attendSet.getAtime6Setother() + "|" + attendSet.getCommute6());
        if (!attendSet.getAtime1Setother().equals("1")) attendSet.setAtime1("");
        if (!attendSet.getAtime2Setother().equals("1")) attendSet.setAtime2("");
        if (!attendSet.getAtime3Setother().equals("1")) attendSet.setAtime3("");
        if (!attendSet.getAtime4Setother().equals("1")) attendSet.setAtime4("");
        if (!attendSet.getAtime5Setother().equals("1")) attendSet.setAtime5("");
        if (!attendSet.getAtime6Setother().equals("1")) attendSet.setAtime6("");
        attendSet.setWorktimeF(seconds(attendSet.getWorktimeF()));
        attendSet.setWorktimeB(seconds(attendSet.getWorktimeB()));
        attendSet.setWorkafterB(seconds(attendSet.getWorkafterB()));
        attendSet.setWorkafterF(seconds(attendSet.getWorkafterF()));
        return strReturn;
    }

    //处理考勤类型实体类
    public static void handleAttendSet(AttendSet attendSet) {
        //拆分上下班开关和 上下班类型
        String[] str1 = attendSet.getAtime1Set().split("\\|");
        attendSet.setAtime1Setother(str1[0]);
        attendSet.setCommute1(str1[1]);
        String[] str2 = attendSet.getAtime2Set().split("\\|");
        attendSet.setAtime2Setother(str2[0]);
        attendSet.setCommute2(str2[1]);
        String[] str3 = attendSet.getAtime3Set().split("\\|");
        attendSet.setAtime3Setother(str3[0]);
        attendSet.setCommute3(str3[1]);
        String[] str4 = attendSet.getAtime4Set().split("\\|");
        attendSet.setAtime4Setother(str4[0]);
        attendSet.setCommute4(str4[1]);
        String[] str5 = attendSet.getAtime5Set().split("\\|");
        attendSet.setAtime5Setother(str5[0]);
        attendSet.setCommute5(str5[1]);
        String[] str6 = attendSet.getAtime6Set().split("\\|");
        attendSet.setAtime6Setother(str6[0]);
        attendSet.setCommute6(str6[1]);
        //汇总上下班开关和 上下班类型，上下班时间
        Map<String, String> map = new HashMap<String, String>();
        map.put("atime1Setother", attendSet.getAtime1Setother());
        map.put("atime2Setother", attendSet.getAtime2Setother());
        map.put("atime3Setother", attendSet.getAtime3Setother());
        map.put("atime4Setother", attendSet.getAtime4Setother());
        map.put("atime5Setother", attendSet.getAtime5Setother());
        map.put("atime6Setother", attendSet.getAtime6Setother());
        Map<String, String> map1 = new HashMap<String, String>();
        map1.put("commute1", attendSet.getCommute1());
        map1.put("commute2", attendSet.getCommute2());
        map1.put("commute3", attendSet.getCommute3());
        map1.put("commute4", attendSet.getCommute4());
        map1.put("commute5", attendSet.getCommute5());
        map1.put("commute6", attendSet.getCommute6());
        Map<String, String> map2 = new HashMap<String, String>();
        map2.put("atime1", attendSet.getAtime1());
        map2.put("atime2", attendSet.getAtime2());
        map2.put("atime3", attendSet.getAtime3());
        map2.put("atime4", attendSet.getAtime4());
        map2.put("atime5", attendSet.getAtime5());
        map2.put("atime6", attendSet.getAtime6());

        attendSet.setAtimeSetotherList(map);
        attendSet.setCommuteList(map1);
        attendSet.setTimeList(map2);
        //ip段分割
        if (!StringUtils.checkNull(attendSet.getIpParagraph())) {
            String[] ipParagraph = attendSet.getIpParagraph().split("-");
            attendSet.setiPStart(ipParagraph[0]);
            if(ipParagraph.length>1){
                attendSet.setiPEnd(ipParagraph[1]);
            }
        }
    }
}
